<?php


namespace App\Common\Logging\Injection;


use App\Jobs\PushLogToEmailJob;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\Facades\Mail;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;

/**
 * 注入到log引擎，监听日志发送数据到email,使用队列机制投送
 * Class LoggerInjection
 * @package App\Common\Logging\Injection
 */
class LoggerPushEmailJobInjection
{
    public function __invoke($logger)
    {
        try {
            //此处用添加监听事件 可以添加多个事件
            $logger->listen(function (MessageLogged $messageLogged){
                $to = env('LOG_PUSH_TO_EMAIL', "");
                if (!isEmail($to)) {
                    return false;
                }
                dispatch((new PushLogToEmailJob(DbObjectToArr($messageLogged)))->onQueue("log_list"));
            });
        } catch (\Exception $e) {

        }
    }
}